package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.criteria.OrderInfoCriteria;
import com.ayf.payment.game.api.entity.MerchantBudget;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantBudgetMapper;
import com.ayf.payment.game.api.mapper.MerchantPayOrderMapper;
import com.ayf.payment.game.api.mapper.OrderMapper;
import com.ayf.payment.game.api.mapper.platform.MerchantCashOutMapper;
import com.ayf.payment.game.api.service.MerchantBudgetService;
import com.ayf.payment.game.api.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MerchantBudgetServiceImpl implements MerchantBudgetService {

    @Resource
    private MerchantBudgetMapper merchantBudgetMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private MerchantCashOutMapper merchantCashOutMapper;

    @Resource
    private MerchantPayOrderMapper merchantPayOrderMapper;

    @Override
    public List<MerchantBudget> select(Integer merchantId) {
        if (merchantId == null) {
            throw new ParameterException("商户id不能为空");
        }
        return merchantBudgetMapper.selectBudgets(merchantId);
    }

    @Override
    public MerchantBudget selectToday(Integer merchantId) {
        if (merchantId == null) {
            throw new ParameterException("商户id不能为空");
        }
        List<Integer> id = new ArrayList<>();
        id.add(merchantId);
        OrderInfoCriteria orderCriteria = new OrderInfoCriteria();
        String today = DateUtils.dateToString(new Date(), DateUtils.formatPattern);
        String yesterday = DateUtils.dateToString(DateUtils.getDayBeforeDay(1), DateUtils.formatPattern);
        orderCriteria.setStartTime(today + " 00:00:00");
        orderCriteria.setMerchantId(merchantId);
        Map<Integer, Map<String, Object>> tradeMap = orderMapper.selectSumAmount(orderCriteria);
        Map<Integer, Map<String, Object>> settleMap = merchantCashOutMapper.selectSumWithdrawal(orderCriteria);
        Map<Integer, Map<String, Object>> balanceMap = merchantBudgetMapper.selectBalance(yesterday, id);
        Map<Integer, Map<String, Object>> payMap = merchantPayOrderMapper.selectSumPay(orderCriteria);
        //代理商收入
        Map<Integer, Map<String, Object>> proxyMap = orderMapper.selectSumProxyIncome(orderCriteria);
        BigDecimal tradeAmount = new BigDecimal(0);
        BigDecimal settleAmount = new BigDecimal(0);
        BigDecimal balance = new BigDecimal(0);
        if (tradeMap.get(merchantId) != null) {
            tradeAmount = (BigDecimal) tradeMap.get(merchantId).get("merchantTotalIncome");
        }
        if (settleMap.get(merchantId) != null) {
            settleAmount = (BigDecimal) settleMap.get(merchantId).get("merchantTotalSettle");
        }
        if (balanceMap.get(merchantId) != null) {
            balance = (BigDecimal) balanceMap.get(merchantId).get("balance");
        }
        if (proxyMap.get(merchantId) != null) {
            tradeAmount = tradeAmount.add((BigDecimal) proxyMap.get(merchantId).get("proxyTotalIncome"));
        }
        if (payMap.get(merchantId) != null) {
            settleAmount = settleAmount.add((BigDecimal) payMap.get(merchantId).get("merchantTotalPay"));
        }
        MerchantBudget merchantBudget = new MerchantBudget();
        merchantBudget.setTradeAmount(tradeAmount);
        merchantBudget.setYesterdayBalance(balance);
        merchantBudget.setMerchantSettleAmount(settleAmount);
        merchantBudget.setBalance(balance.add(tradeAmount).subtract(settleAmount));
        merchantBudget.setSettleDate(today);
        merchantBudget.setRefundAmount(new BigDecimal(0));
        return merchantBudget;
    }

    @Override
    public boolean addBudgets(List<Integer> subList, Map<Integer, Map<String, Object>> tradeMap, Map<Integer, Map<String,
            Object>> settleMap, Map<Integer, Map<String, Object>> proxyMap, Map<Integer, Map<String, Object>> payMap) {
        List<MerchantBudget> list = new ArrayList<>();
        String date = DateUtils.dateToString(DateUtils.getDayBeforeDay(1), DateUtils.formatPattern);
        Map<Integer, Map<String, Object>> balanceMap = merchantBudgetMapper.selectBalance(date, subList);
        for (Integer merchantId : subList) {
            MerchantBudget merchantBudget = new MerchantBudget();
            merchantBudget.setMerchantId(merchantId);
            BigDecimal tradeAmount = BigDecimal.ZERO;
            BigDecimal settleAmount = BigDecimal.ZERO;
            BigDecimal balance = BigDecimal.ZERO;
            if (tradeMap.get(merchantId) != null) {
                tradeAmount = (BigDecimal) tradeMap.get(merchantId).get("merchantTotalIncome");
            }
            if (proxyMap.get(merchantId) != null) {
                tradeAmount = tradeAmount.add((BigDecimal) proxyMap.get(merchantId).get("proxyTotalIncome"));
            }
            if (settleMap.get(merchantId) != null) {
                settleAmount = (BigDecimal) settleMap.get(merchantId).get("merchantTotalSettle");
            }
            if (payMap.get(merchantId) != null) {
                settleAmount = settleAmount.add((BigDecimal) payMap.get(merchantId).get("merchantTotalPay"));
            }
            if (balanceMap.get(merchantId) != null) {
                balance = (BigDecimal) balanceMap.get(merchantId).get("balance");
            }
            merchantBudget.setTradeAmount(tradeAmount);
            merchantBudget.setYesterdayBalance(balance);
            merchantBudget.setMerchantSettleAmount(settleAmount);
            merchantBudget.setBalance(balance.add(tradeAmount).subtract(settleAmount));
            merchantBudget.setSettleDate(date);
            merchantBudget.setRefundAmount(new BigDecimal(0));
            list.add(merchantBudget);
        }
        merchantBudgetMapper.insertBudgets(list);
        return true;
    }
}
